package com.laikan.reader.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.comment.Constants;
import com.socks.library.KLog;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信支付成功回调类
 *
 * @author Administrator
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        finish();
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            int code = resp.errCode;
            switch (code) {
                case BaseResp.ErrCode.ERR_OK:

                    if (SPUtil.getBoolean("signedAction", false)) {
                        LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("action_signed"));
                    }
                    Toast.makeText(this, "支付成功!", Toast.LENGTH_LONG).show();
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Toast.makeText(this, "支付取消!", Toast.LENGTH_LONG).show();
                    break;
                default:
                    Toast.makeText(this, "支付失败!", Toast.LENGTH_LONG).show();
                    break;

            }
            KLog.e("50 微信支付-2  resp.errCode = " + resp.errCode);
        }
    }
}